package org.kabeja.parser;

import org.kabeja.dxf.DXFConstants;
import org.kabeja.dxf.DXFDocument;
import org.kabeja.dxf.entity.DXFEntity;
import org.kabeja.parser.entities.DXFEntityHandler;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shutao
 * @since 2022/4/25 10:49
 */
public class DXFEntitiesSectionHandler extends AbstractSectionHandler implements DXFSectionHandler, HandlerManager {
    private static String SECTION_KEY = "ENTITIES";
    public static final int ENTITY_START = 0;
    public static final int OBJECT_CODE = 102;
    protected Map<String, Object> handlers = new HashMap<>();
    protected DXFEntityHandler handler = null;
    protected boolean parseEntity = false;

    /** 写入对象类型：0，退出对象写入；1，其它对象写入；>1，对应对象写入 */
    private static final String ACAD_CLOSE = "}";
    private int writeObjectName = 0;

    public DXFEntitiesSectionHandler() {
    }

    @Override
    public String getSectionKey() {
        return SECTION_KEY;
    }

    @Override
    public void parseGroup(int var1, DXFValue var2) {
        if (var1 == ENTITY_START) {
            if (this.parseEntity) {
                if (this.handler.isFollowSequence()) {
                    this.handler.parseGroup(var1, var2);
                    return;
                }

                this.endEntity();
            }

            if (this.handlers.containsKey(var2.getValue())) {
                this.handler = (DXFEntityHandler)this.handlers.get(var2.getValue());
                this.handler.setDXFDocument(this.doc);
                this.handler.startDXFEntity();
                this.parseEntity = true;
            } else {
                this.parseEntity = false;
            }
        } else if (this.parseEntity) {
            if (var1 == OBJECT_CODE) {
                // 102图元特殊处理，应用程序定义的组的开始“{application_name” 和结束 “}”
                System.out.println("程序化对象程序：" + var2.getValue());
                switch (var2.getValue()) {
                    case DXFConstants.ACAD_XDICTIONARY_OPEN:
                        writeObjectName = DXFConstants.ACAD_XDICTIONARY_CODE;
                        break;
                    case DXFConstants.ACAD_REACTORS_OPEN:
                        writeObjectName = DXFConstants.ACAD_REACTORS_CODE;
                        break;
                    case ACAD_CLOSE:
                        writeObjectName = 0;
                        break;
                    default:
                        System.out.println("其它程序化对象程序：" + var2.getValue());
                        writeObjectName = 1;
                }
                return;
            }
            if (writeObjectName > 0) {
                // 读取图元的 object信息
                this.handler.parseObject(var1, var2, writeObjectName);
            } else {
                // 读取图元的基础信息
                this.handler.parseGroup(var1, var2);
            }
        }

    }

    @Override
    public void setDXFDocument(DXFDocument var1) {
        this.doc = var1;
    }

    @Override
    public void endSection() {
        this.endEntity();
    }

    @Override
    public void startSection() {
        this.parseEntity = false;
    }

    protected void endEntity() {
        if (this.parseEntity) {
            this.handler.endDXFEntity();
            DXFEntity var1 = this.handler.getDXFEntity();
            this.doc.addDXFEntity(var1);
        }

    }

    public void addDXFEntityHandler(DXFEntityHandler var1) {
        var1.setDXFDocument(this.doc);
        this.handlers.put(var1.getDXFEntityName(), var1);
    }

    @Override
    public void addHandler(Handler var1) {
        this.addDXFEntityHandler((DXFEntityHandler)var1);
    }

    @Override
    public void releaseDXFDocument() {
        this.doc = null;
        for (Object o : this.handlers.values()) {
            Handler var2 = (Handler) o;
            var2.releaseDXFDocument();
        }

    }
}
